package com.emns.web.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.emns.pojo.User;
import com.emns.pojo.Validate;
import com.emns.service.user.UserService;
import common.StringSecurity;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	UserService us;

	/**
	 * 登陆
	 * 
	 * @param model
	 * @param uname
	 * @param pass
	 * @param certCode
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(Model model, String uname, String pass,
			String certCode, HttpServletRequest request) {
		HttpSession session = request.getSession();
		String cCode = (String) session.getAttribute("certCode");
		String md5Pwd = StringSecurity.encode(pass);
		if (certCode.equals(cCode)) {
			User user = us.login(uname, md5Pwd);
			if (user != null) {
				return "/main";
			} else {
				return "";
			}
		} else {
			return "";
		}
		// request

	}

	/**
	 * 修改密码
	 * 
	 * @param model
	 * @param newpass
	 * @param uid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/changePass", method = RequestMethod.POST)
	public String changePass(Model model, String newpass, int uid,
			HttpServletRequest request) {
		int result = us.changPassword(newpass, uid);
		if (result > 0) {
			return "";
		} else {
			return "";
		}

	}

	/**
	 * 修改有效期
	 * 
	 * @param model
	 * @param month
	 * @return
	 */
	@RequestMapping(value = "/changeValiDate", method = RequestMethod.POST)
	public String changeValiDate(Model model, int month) {
		us.updateMonth(month);

		return "redirect:/user/queryValiDate";

	}

	@RequestMapping(value = "/queryValiDate")
	public String queryValiDate(Model model) {

		Validate v = us.queryValidate();
		model.addAttribute("vDate", v);
		return "/sys_time_limit";

	}
}
